﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using MiragePWF.NH.Logging;
using MiragePWF.NH.Repositories;
using NHibernate.Criterion;
using Wolfut.MirageEE.Domain.Common;
using Wolfut.MirageEE.Domain.CommonUtilities;
using Wolfut.MirageEE.Interface.Common;

namespace Wolfut.MirageEE.Server.Server.Common
{
    [Transactional]
    [AutoLogging]
    public class ProjectsService : IProjectsService
    {
        private readonly IRepository<Project> projectRepository;
        public ProjectsService(IRepository<Project> projectRepository)
        {
            this.projectRepository = projectRepository;
        }
        [Transaction(TransactionMode.Requires)]
        public Project AddOrUpdateProject(Project project)
        {
            projectRepository.Save(project);
            return project;
        }
        [Transaction(TransactionMode.Requires)]
        public Project UpdateProject(Project project)
        {
            this.projectRepository.Save(project);
            return project;
        }
        public Project GetProjectInfoByProjectCode(String projectCode)
        {
            return projectRepository.FindFirst(Restrictions.Eq("ProjectCode", projectCode));
        }
        [Transaction(TransactionMode.Requires)]
        public void DeleteProject(String projectCode)
        {
            Project project = this.GetProjectInfoByProjectCode(projectCode);
            project.IsDeleted = true;
            projectRepository.Save(project);
        }
        public void DeleteAllProject(IList<String> listProjectID)
        {
            foreach (String projectID in listProjectID)
            {
                Project item=projectRepository.Find(projectID);
                item.IsDeleted = true;
                projectRepository.Update(item);
            }
        }
        public IList<Project> ListAllProject(bool isDeleted)
        {
            DetachedCriteria criteria = DetachedCriteria.For(typeof(Project));
            criteria.Add(Expression.Eq("IsDeleted", isDeleted));
            criteria.AddOrder(new Order("ProjectCode", true));
            return this.projectRepository.FindAll(criteria);
        }        
    }
}